package com.guanzhi.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.guanzhi.mp.mapper.BookMapper;
import com.guanzhi.mp.po.BookPo;
import com.guanzhi.mp.service.BookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Mybatis-Plus 练习
 * <p> Package: com.guanzhi.mp.service.impl </p>
 * <p> File: BookServiceImpl.java </p>
 * <p> Description:  (接口服务实现类)</p>
 * <p> Date: 2024/6/15 21:43</p>
 *
 * @author 观之
 * @version 1.0 （代码版本）
 * @email <a href="mailto:guanzhi55634@aliyun.com">观之</a>（邮箱）
 * @date 2024/6/15 21:43
 * @since jdk17（jdk版本）
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {

    private final BookMapper bookMapper;

    @Override
    public List<BookPo> queryWrapper() {
        //使用QueryWrapper模糊查询数据
        QueryWrapper<BookPo> queryWrapper = new QueryWrapper<>();
        //模糊查询,作者姓张的 信息
        queryWrapper.like("author", "张");
        //执行查询
        return bookMapper.selectList(queryWrapper);
    }

    @Override
    public void updateWrapper() {
        //使用UpdateWrapper更新数据
        UpdateWrapper<BookPo> updateWrapper = new UpdateWrapper<>();
        //条件,主键为1
        updateWrapper.eq("id", 1);
        //更新字段
        updateWrapper.set("name", "Java编程思想");
        //执行更新
        bookMapper.update(updateWrapper);
    }

    @Override
    public List<BookPo> lambdaQueryWrapper() {
        //使用LambdaQueryWrapper模糊查询数据
        LambdaQueryWrapper<BookPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //模糊查询,作者姓张的 信息
        //同时解决硬编码问题
        lambdaQueryWrapper.like(BookPo::getAuthor, "张");
        return bookMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<BookPo> customQuery() {
        //使用LambdaQueryWrapper模糊查询数据
        LambdaQueryWrapper<BookPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //模糊查询,作者姓张的 信息
        //同时解决硬编码问题
        lambdaQueryWrapper.like(BookPo::getAuthor, "张");
        return bookMapper.customQuery(lambdaQueryWrapper);
    }
}
